let { tool, DB, iconv, cheerio } = require("../common/main");
let request = require("request");
const codeConfig = {//短信平台配置
    'xmid': 45561,
    'rj': '15865578882',
    'token': '132FC39318F61B628586FCA6019D12F3'
};
let conTime = "";
const getCodeConfig = () => {
    request({
        method: 'get',
        url: `http://www.mili18.com:9180/service.asmx/UserLoginStr?name=15865578882&psw=q6311719q`,
    }, (error, response, body) => {
        clearTimeout(conTime);
        conTime = setTimeout(() => {
            getCodeConfig();
        }, 1000 * 60 * 30);//一小时调用一次
        if (!error) {
            //let str = body.split('&');
            console.log("取得开发者token=>", body);
            codeConfig.token = body;
        }
    })
};
getCodeConfig();
module.exports = {
    /**
     * @description 获取手机号码
     * @param number count 获取号码数量
     * @author 蜂鸟配送
     */
    getPhone: (count = 1, hm = "") => {
        return new Promise((resolve, reject) => {
            // let Areas = ["陕西", "湖南", "四川"];
            //let Areas = ["陕西", "四川"];
            //let sv = Areas[tool.randomNumBoth(0, Areas.length - 1)];
            let types = tool.randomNumBoth(1, 3);
            console.log(`运营商:${types},${tool.date()}`);
            let data = { ...codeConfig, sl: count, hm, lx: types, a1: "", a2: "", pk: "", ks: 0 };
            tool.$ajax({
                type: "get",
                url: `http://www.mili18.com:9180/service.asmx/${!hm ? 'GetHM2Str' : 'mkHM2Str'}`,
                data: data,
                success: res => {
                    if (res === 0) {
                        getCodeConfig();
                    }
                    // console.log(res, sv);
                    resolve(res);
                },
                error: res => {
                    reject(res);
                }
            })
        });
    },
    /**
     * @description 监听手机号码短信
     * @param number phone 号码
     * @author 蜂鸟配送
     */
    getPhoneCode: phone => {
        return new Promise((resolve, reject) => {
            tool.$ajax({
                type: "get",
                url: `http://www.mili18.com:9180/service.asmx/GetYzm2Str`,
                form: true,
                data: { ...codeConfig, hm: phone, sf: 1 },
                success: res => {
                    resolve(res);
                },
                error: res => {
                    reject(res);
                }
            })
        });
    },
    /**
     * @description 加黑号码
     * @param number phone 号码
     * @author 蜂鸟配送
     */
    Addblack: phone => {
        return new Promise((resolve, reject) => {
            tool.$ajax({
                type: "get",
                url: `http://www.mili18.com:9180/service.asmx/Hmd2Str`,
                form: true,
                data: { ...codeConfig, hm: phone, sf: 1 },
                success: res => {
                    resolve(res);
                },
                error: res => {
                    reject(res);
                }
            })
        });
    },
    /**
     * @description 释放号码
     * @param number phone 号码
     * @author 蜂鸟配送
     */
    clearPhone: phone => {
        return new Promise((resolve, reject) => {
            tool.$ajax({
                type: "get",
                url: `http://www.mili18.com:9180/service.asmx/clearPhone`,
                form: true,
                data: { ...codeConfig, hm: phone },
                success: res => {
                    resolve(res);
                },
                error: res => {
                    reject(res);
                }
            })
        });
    },

};
